﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Design;

namespace VisualPureBasicIDE.PureBasic.Forms.Gadgets
{
    [Serializable]
    public partial class ButtonGadget : UserControl
    {
        string _myID;
        bool _generateStandardEventHandler;
        ButtonGadgetFlags _myFlags;

        public ButtonGadget()
        {
            InitializeComponent();
            GadgetID = '#' + Name;
            Text = "New ButtonGadget";
            _myFlags = new ButtonGadgetFlags(gadget);
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        public ButtonGadget(string gadgetID)
        {
            InitializeComponent();
            if (gadgetID != null && gadgetID[0] == '#') { _myID = gadgetID; } else { throw new ArgumentException("Illegal GadgetID"); }
            Text = "New ButtonGadget";
            _myFlags = new ButtonGadgetFlags(gadget);
        }

        public string GadgetID
        {
            get { return _myID; }
            set { if (value != null && value[0] == '#') _myID = value; }
        }

        public string GadgetText
        {
            get { return Text; }
            set { Text = value; }
        }

        public int X
        {
            get { return Left; }
            set { if (value > 0) Left = value; }
        }

        public int Y
        {
            get { return Top; }
            set { if (value < 0) Top = value; }
        }

        public int GadgetWidth
        {
            get { return Width; }
            set { if (value >= 0) Width = value; }
        }

        public int GadgetHeight
        {
            get { return Height; }
            set { if (value >= 0) Height = value; }
        }

        [Description("The flags of the gadget"), Editor(typeof(FlagEditor), typeof(UITypeEditor))]
        public ButtonGadgetFlags Flags
        {
            get { return _myFlags; }
            set 
            { 
                _myFlags = value;
            }
        }

        #region IGadget Members

        public bool GenerateStandardEventHandler
        {
            get
            {
                return _generateStandardEventHandler;
            }
            set
            {
                _generateStandardEventHandler = value;
            }
        }
        #endregion
    }

    /// <summary>
    /// Edits the flags of the gadget
    /// </summary>
    public class FlagEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            // Use IWindowsFormsEditorService object to display a control in the dropdown area
            var frmsvr = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (frmsvr == null)
                return null;
            var ui = new FlagEditorUi();
            var flags = (ButtonGadgetFlags)value;
            ui.chkDefault.Checked = flags.Default;
            ui.chkLeft.Checked = flags.Left;
            ui.chkMultiLine.Checked = flags.MultiLine;
            ui.chkRight.Checked = flags.Right;
            ui.chkToggle.Checked = flags.Toggle;

            frmsvr.DropDownControl(ui);

            flags.Default = ui.chkDefault.Checked;
            flags.Left = ui.chkLeft.Checked;
            flags.MultiLine = ui.chkMultiLine.Checked;
            flags.Right = ui.chkRight.Checked;
            flags.Toggle = ui.chkToggle.Checked;
            return flags;
        }
    }

    public class ButtonGadgetFlags
    {
        private bool _right, _left, _default, _multiLine, _toggle;
        private readonly Button _gadget;

        public ButtonGadgetFlags(Button gadget)
        {
            _gadget = gadget;
        }

        public override string ToString()
        {
            var buffer = String.Empty;
            if (_right) buffer += "Right; ";
            if (_left) buffer += "Left; ";
            if (_default) buffer += "Default; ";
            if (_multiLine) buffer += "MultiLine; ";
            if (_toggle) buffer += "Toggle; ";
            if (buffer != String.Empty) buffer = buffer.Remove(buffer.Length - 2);
            return buffer;
        }

        public bool Right
        {
            get { return _right; }
            set 
            { 
                _right = value; 
                if(_right) _gadget.TextAlign = ContentAlignment.MiddleRight;
                else if (!_right && !_left) _gadget.TextAlign = ContentAlignment.MiddleCenter;
            }
        }

        public bool Left
        {
            get { return _left; }
            set 
            { 
                _left = value;
                if (_left) _gadget.TextAlign = ContentAlignment.MiddleLeft;
                else if (!_right && !_left) _gadget.TextAlign = ContentAlignment.MiddleCenter;
            }
        }

        public bool Default
        {
            get { return _default; }
            set { _default = value; }
        }

        public bool MultiLine
        {
            get { return _multiLine; }
            set { _multiLine = value; }
        }

        public bool Toggle
        {
            get { return _toggle; }
            set { _toggle = value; }
        }
    }
}